package com.sugar.subscribe.course.module.business.service.serviceclass;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sugar.subscribe.course.module.business.controller.app.serviceclass.vo.AppServiceClassRespVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import com.sugar.subscribe.course.module.business.controller.admin.serviceclass.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.serviceclass.ServiceClassDO;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;

import com.sugar.subscribe.course.module.business.dal.mysql.serviceclass.ServiceClassMapper;

import static com.sugar.subscribe.course.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.subscribe.course.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.subscribe.course.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.subscribe.course.module.business.enums.ErrorCodeConstants.*;

/**
 * 服务分类 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class ServiceClassServiceImpl implements ServiceClassService {

    @Resource
    private ServiceClassMapper serviceClassMapper;

    @Override
    public Long createServiceClass(ServiceClassSaveReqVO createReqVO) {
        // 插入
        ServiceClassDO serviceClass = BeanUtils.toBean(createReqVO, ServiceClassDO.class);
        serviceClassMapper.insert(serviceClass);

        // 返回
        return serviceClass.getId();
    }

    @Override
    public void updateServiceClass(ServiceClassSaveReqVO updateReqVO) {
        // 校验存在
        validateServiceClassExists(updateReqVO.getId());
        // 更新
        ServiceClassDO updateObj = BeanUtils.toBean(updateReqVO, ServiceClassDO.class);
        serviceClassMapper.updateById(updateObj);
    }

    @Override
    public void deleteServiceClass(Long id) {
        // 校验存在
        validateServiceClassExists(id);
        // 删除
        serviceClassMapper.deleteById(id);
    }

    @Override
        public void deleteServiceClassListByIds(List<Long> ids) {
        // 删除
        serviceClassMapper.deleteByIds(ids);
        }


    private ServiceClassDO validateServiceClassExists(Long id) {
        ServiceClassDO serviceClassDO = serviceClassMapper.selectById(id);
        if (serviceClassDO == null) {
            throw exception(SERVICE_CLASS_NOT_EXISTS);
        }
        return serviceClassDO;
    }

    @Override
    public ServiceClassDO getServiceClass(Long id) {
        return validateServiceClassExists(id);
    }

    @Override
    public PageResult<ServiceClassDO> getServiceClassPage(ServiceClassPageReqVO pageReqVO) {
        return serviceClassMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AppServiceClassRespVO> getServiceClassAll() {
        return BeanUtils.toBean(serviceClassMapper.selectList(Wrappers.<ServiceClassDO>lambdaQuery()
                .orderByDesc(ServiceClassDO::getSort)), AppServiceClassRespVO.class);
    }

    @Override
    public void updateServiceClassStatus(ServiceClassUpdateStatusReqVO updateReqVO) {
        serviceClassMapper.update(ServiceClassDO::getId, updateReqVO.getId(), ServiceClassDO::getHomeRecommend, updateReqVO.getHomeRecommend());
    }

    @Override
    public List<AppServiceClassRespVO> getHomeServiceClass() {
        return BeanUtils.toBean(serviceClassMapper.selectList(Wrappers.<ServiceClassDO>lambdaQuery()
                .eq(ServiceClassDO::getHomeRecommend, 1).orderByDesc(ServiceClassDO::getSort)), AppServiceClassRespVO.class);
    }

    @Override
    public List<AppServiceClassRespVO> getServiceClassByParentId(Long parentId) {
        return BeanUtils.toBean(serviceClassMapper.selectList(Wrappers.<ServiceClassDO>lambdaQuery()
                .eq(ServiceClassDO::getParentId, parentId).orderByDesc(ServiceClassDO::getSort)), AppServiceClassRespVO.class);
    }

    @Override
    public List<AppServiceClassRespVO> getServiceClassTree() {
        List<AppServiceClassRespVO> serviceClassList = BeanUtils.toBean(serviceClassMapper.selectList(), AppServiceClassRespVO.class);

        Map<Long, List<AppServiceClassRespVO>> serviceClassMap = serviceClassList.stream().collect(Collectors.groupingBy(AppServiceClassRespVO::getParentId));

        List<AppServiceClassRespVO> topServiceClassList = serviceClassMap.get(0L);

        for (AppServiceClassRespVO respVO : topServiceClassList) {
            respVO.setChildren(serviceClassMap.get(respVO.getId()));
        }

        return topServiceClassList;
    }

    @Override
    public List<AppServiceClassRespVO> getTwoServiceClass() {
        return BeanUtils.toBean(
                serviceClassMapper.selectList(Wrappers.<ServiceClassDO>lambdaQuery().ne(ServiceClassDO::getParentId, 0))
                , AppServiceClassRespVO.class);
    }

}